package com.weixin.base.web;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

import com.weixin.base.service.AreaManagerService;
import com.weixin.base.util.ExtJsPageHelper;
import com.weixin.base.util.ListRange;
import com.weixin.base.util.Tools;
import com.weixin.entity.TbArea;
import com.weixin.entity.TbCenterNorelArea;
import com.weixin.entity.TbMobileRelArea;

@Controller
public class AreaManagerAction {
	@Autowired
	private AreaManagerService areaManagerService;
	
	/**
	 * 短信中心号查询分页查询
	 */
	@RequestMapping(value= "/module/main/searchAreaCenter.do",method = RequestMethod.POST)
	public void searchAreaCenter(HttpServletRequest request,HttpServletResponse response)throws IOException {
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, null);
		Page page = areaManagerService.searchAreaCenterByPageReq(pr);
		List tblist = (List) page.getResult();
		ListRange resultList = new ListRange();
		resultList.setList(tblist);
		resultList.setTotalSize(page.getTotalCount());
		resultList.setSumTotal(page.getSumTotal());
		resultList.setPageSize(page.getPageSize());
		resultList.setPagecount(page.getLastPageNumber());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		String result=new ObjectMapper().defaultPrettyPrintingWriter().writeValueAsString(resultList);
		response.getWriter().print(result);
	}
	
	/**
	 * 短信中心号修改
	 */
	@RequestMapping(value= "/module/main/updateAreaCenter.do",method = RequestMethod.POST)
	public void updateAreaCenter(HttpServletRequest request,HttpServletResponse response)throws IOException {
		
		String ncenterNoRelAreaId=request.getParameter("ncenterNoRelAreaId");
		String scenterNo=request.getParameter("scenterNo");
//		String sprovinceName=request.getParameter("sprovinceName");
//		String sprovinceCode=request.getParameter("sprovinceCode");
//		String nprovinceId=request.getParameter("nprovinceId");
//		String scityName=request.getParameter("scityName");
//		String scityCode=request.getParameter("scityCode");
//		String ncityId=request.getParameter("ncityId");
		String nop=request.getParameter("nop");
		
		boolean result=false;
		TbCenterNorelArea tbCenterNorelArea=new TbCenterNorelArea();
		
		try {
			tbCenterNorelArea.setNCenterNoRelAreaId(Tools.convertRi(ncenterNoRelAreaId));
			tbCenterNorelArea.setSCenterNo(Tools.convertRs(scenterNo));
//			tbCenterNorelArea.setNCityId(Tools.convertRi(ncityId));
			tbCenterNorelArea.setNOP(Tools.convertRi(nop));
//			tbCenterNorelArea.setNProvinceId(Tools.convertRi(nprovinceId));
//			tbCenterNorelArea.setSCityCode(Tools.convertRs(scityCode));
//			tbCenterNorelArea.setSCityName(Tools.convertRs(scityName));
//			tbCenterNorelArea.setSProvinceCode(Tools.convertRs(sprovinceCode));
//			tbCenterNorelArea.setSProvinceName(Tools.convertRs(sprovinceName));
			
			result=areaManagerService.updateAreaCenter(tbCenterNorelArea);
			
		} catch (Exception e) {
			result=false;
			e.printStackTrace();
		}
		
		response.getWriter().print(result);
	}
	
	/**
	 * 短信中心号添加
	 */
	@RequestMapping(value= "/module/main/addAreaCenter.do",method = RequestMethod.POST)
	public void addAreaCenter(HttpServletRequest request,HttpServletResponse response)throws IOException {
		
		String scenterNo=request.getParameter("scenterNo");
		String sprovinceName=request.getParameter("sprovinceName");
		String sprovinceCode=request.getParameter("sprovinceCode");
		String nprovinceId=request.getParameter("nprovinceId");
		String scityName=request.getParameter("scityName");
		String scityCode=request.getParameter("scityCode");
		String ncityId=request.getParameter("ncityId");
		String sCode=request.getParameter("scenterNo");
		String nop=request.getParameter("nop");
		
		boolean result=false;
		TbCenterNorelArea tbCenterNorelArea=new TbCenterNorelArea();
		
		try {
			tbCenterNorelArea.setSCenterNo(Tools.convertRs(scenterNo));
			tbCenterNorelArea.setNCityId(Tools.convertRi(ncityId));
			tbCenterNorelArea.setNOP(Tools.convertRi(nop));
			tbCenterNorelArea.setNProvinceId(Tools.convertRi(nprovinceId));
			tbCenterNorelArea.setSCityCode(Tools.convertRs(scityCode));
			tbCenterNorelArea.setSCityName(Tools.convertRs(scityName));
			tbCenterNorelArea.setSProvinceCode(Tools.convertRs(sprovinceCode));
			tbCenterNorelArea.setSProvinceName(Tools.convertRs(sprovinceName));
			
			result=areaManagerService.addAreaCenter(tbCenterNorelArea);
			
		} catch (Exception e) {
			result=false;
			e.printStackTrace();
		}
		
		response.getWriter().print(result);
	}
	
	/**
	 * 短信中心号删除
	 */
	@RequestMapping(value= "/module/main/deleteAreaCenter.do",method = RequestMethod.POST)
	public void deleteAreaCenter(HttpServletRequest request,HttpServletResponse response)throws IOException {
		
		String ncenterNoRelAreaId=request.getParameter("ncenterNoRelAreaId");
		
		boolean result=false;
		
		try {
			result=areaManagerService.deleteAreaCenter(Tools.convertRi(ncenterNoRelAreaId));
			
		} catch (Exception e) {
			result=false;
			e.printStackTrace();
		}
		
		response.getWriter().print(result);
	}
	
	
	/**
	 * 手机号段查询分页查询areaMoblie.js
	 */
	@RequestMapping(value= "/module/main/searchAreaMoblie.do",method = RequestMethod.POST)
	public void searchAreaMoblie(HttpServletRequest request,HttpServletResponse response)throws IOException {
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, null);
		Page page = areaManagerService.searchAreaMoblieByPageReq(pr);
		List tblist = (List) page.getResult();
		ListRange resultList = new ListRange();
		resultList.setList(tblist);
		resultList.setTotalSize(page.getTotalCount());
		resultList.setSumTotal(page.getSumTotal());
		resultList.setPageSize(page.getPageSize());
		resultList.setPagecount(page.getLastPageNumber());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		String result=new ObjectMapper().defaultPrettyPrintingWriter().writeValueAsString(resultList);
		response.getWriter().print(result);
	}
	
	/**
	 * 手机号段修改
	 */
	@RequestMapping(value= "/module/main/updateAreaMoblie.do",method = RequestMethod.POST)
	public void updateAreaMoblie(HttpServletRequest request,HttpServletResponse response)throws IOException {
		
		String nmobileRelAreaId=request.getParameter("nmobileRelAreaId");
		String smoblieNo=request.getParameter("smoblieNo");
//		String sprovinceName=request.getParameter("sprovinceName");
//		String sprovinceCode=request.getParameter("sprovinceCode");
//		String nprovinceId=request.getParameter("nprovinceId");
//		String scityName=request.getParameter("scityName");
//		String scityCode=request.getParameter("scityCode");
//		String ncityId=request.getParameter("ncityId");
		String nop=request.getParameter("nop");
		
		boolean result=false;
		TbMobileRelArea tbMobileRelArea=new TbMobileRelArea();
		
		try {
			tbMobileRelArea.setNMobileRelAreaId(Tools.convertRi(nmobileRelAreaId));
			tbMobileRelArea.setSMoblieNo(Tools.convertRs(smoblieNo));
//			tbMobileRelArea.setNCityId(Tools.convertRi(ncityId));
			tbMobileRelArea.setNOP(Tools.convertRi(nop));
//			tbMobileRelArea.setNProvinceId(Tools.convertRi(nprovinceId));
//			tbMobileRelArea.setSCityCode(Tools.convertRs(scityCode));
//			tbMobileRelArea.setSCityName(Tools.convertRs(scityName));
//			tbMobileRelArea.setSProvinceCode(Tools.convertRs(sprovinceCode));
//			tbMobileRelArea.setSProvinceName(Tools.convertRs(sprovinceName));
			
			result=areaManagerService.updateAreaMoblie(tbMobileRelArea);
			
		} catch (Exception e) {
			result=false;
			e.printStackTrace();
		}
		
		response.getWriter().print(result);
	}
	
	/**
	 * 手机号段添加
	 */
	@RequestMapping(value= "/module/main/addAreaMoblie.do",method = RequestMethod.POST)
	public void addAreaMoblie(HttpServletRequest request,HttpServletResponse response)throws IOException {
		
		String smoblieNo=request.getParameter("smoblieNo");
		String sprovinceName=request.getParameter("sprovinceName");
		String sprovinceCode=request.getParameter("sprovinceCode");
		String nprovinceId=request.getParameter("nprovinceId");
		String scityName=request.getParameter("scityName");
		String scityCode=request.getParameter("scityCode");
		String ncityId=request.getParameter("ncityId");
		String nop=request.getParameter("nop");
		
		boolean result=false;
		TbMobileRelArea tbMobileRelArea=new TbMobileRelArea();
		
		try {
			tbMobileRelArea.setSMoblieNo(Tools.convertRs(smoblieNo));
			tbMobileRelArea.setNCityId(Tools.convertRi(ncityId));
			tbMobileRelArea.setNOP(Tools.convertRi(nop));
			tbMobileRelArea.setNProvinceId(Tools.convertRi(nprovinceId));
			tbMobileRelArea.setSCityCode(Tools.convertRs(scityCode));
			tbMobileRelArea.setSCityName(Tools.convertRs(scityName));
			tbMobileRelArea.setSProvinceCode(Tools.convertRs(sprovinceCode));
			tbMobileRelArea.setSProvinceName(Tools.convertRs(sprovinceName));
			
			result=areaManagerService.addAreaMoblie(tbMobileRelArea);
			
		} catch (Exception e) {
			result=false;
			e.printStackTrace();
		}
		
		response.getWriter().print(result);
	}
	
	/**
	 * 手机号段删除
	 */
	@RequestMapping(value= "/module/main/deleteAreaMoblie.do",method = RequestMethod.POST)
	public void deleteAreaMoblie(HttpServletRequest request,HttpServletResponse response)throws IOException {
		
		String nmobileRelAreaId=request.getParameter("nmobileRelAreaId");
		
		boolean result=false;
		
		try {
			result=areaManagerService.deleteAreaMoblie(Tools.convertRi(nmobileRelAreaId));
			
		} catch (Exception e) {
			result=false;
			e.printStackTrace();
		}
		
		response.getWriter().print(result);
	}
	
	/**
	 * 地市管理分页查询
	 */
	@RequestMapping(value= "/module/main/searchareaManager.do",method = RequestMethod.POST)
	public void searchareaManager(HttpServletRequest request,HttpServletResponse response)throws IOException {
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, null);
		Page page = areaManagerService.searchareaManager(pr);
		List tblist = (List) page.getResult();
		ListRange resultList = new ListRange();
		resultList.setList(tblist);
		resultList.setTotalSize(page.getTotalCount());
		resultList.setSumTotal(page.getSumTotal());
		resultList.setPageSize(page.getPageSize());
		resultList.setPagecount(page.getLastPageNumber());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		String result=new ObjectMapper().defaultPrettyPrintingWriter().writeValueAsString(resultList);
		response.getWriter().print(result);
	}
	
	/**
	 * 地市修改
	 */
	@RequestMapping(value= "/module/main/updateareaManager.do",method = RequestMethod.POST)
	public void updateareaManager(HttpServletRequest request,HttpServletResponse response)throws IOException {
		
		boolean result=false;
		TbArea tbArea=new TbArea();
		
		try {
			tbArea.setNAreaId(Tools.convertRi(request.getParameter("nareaId")));
			tbArea.setNPId(Tools.convertRi(request.getParameter("nprovinceId")));
			tbArea.setSName(Tools.convertRs(request.getParameter("scityName")));
			tbArea.setSCode(Tools.convertRs(request.getParameter("scityCode")));
			
			result=areaManagerService.updateareaManager(tbArea);
			
		} catch (Exception e) {
			result=false;
			e.printStackTrace();
		}
		
		response.getWriter().print(result);
	}
	
	/**
	 * 地市添加
	 */
	@RequestMapping(value= "/module/main/addareaManager.do",method = RequestMethod.POST)
	public void addareaManager(HttpServletRequest request,HttpServletResponse response)throws IOException {
		
		boolean result=false;
		TbArea tbArea=new TbArea();
		
		try {
			tbArea.setNPId(Tools.convertRi(request.getParameter("nprovinceId")));
			tbArea.setSName(Tools.convertRs(request.getParameter("scityName")));
			tbArea.setSCode(Tools.convertRs(request.getParameter("scityCode")));
			
			result=areaManagerService.addareaManager(tbArea);
			
		} catch (Exception e) {
			result=false;
			e.printStackTrace();
		}
		
		response.getWriter().print(result);
	}
	
	/**
	 * 查询所有省份
	 */
	@RequestMapping(value = "/module/main/selectsProvince.do")
	public void selectsProvince(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		List<Map<String, Object>> sProvincelist = areaManagerService
				.selectsProvince();
		ObjectMapper objm = new ObjectMapper();
		String result = objm.defaultPrettyPrintingWriter().writeValueAsString(
				sProvincelist);
		response.getWriter().print(result);
	}

	/**
	 * 查询某个省份所有城市
	 */
	@RequestMapping(value = "/module/main/selectsCitybyProvince.do")
	public void selectsCitybyProvince(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String sProvinceId =request.getParameter("nProvinceId");
		int nProvinceId = sProvinceId==null?0:Integer.parseInt(sProvinceId);
		List<Map<String, Object>> sCitylist = areaManagerService
				.selectsCitybyProvince(nProvinceId);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("nAreaId", "0");
		map.put("sName", "全部");
		map.put("sCode", "");
		sCitylist.add(0,map);
		ObjectMapper objm = new ObjectMapper();
		String result = objm.defaultPrettyPrintingWriter().writeValueAsString(
				sCitylist);
		response.getWriter().print(result);
	}
	
	/**
	 * 查询某个省份所有城市 用于批量修改
	 */
	@RequestMapping(value = "/module/main/selectsCitysbyProvince.do")
	public void selectsCitysbyProvince(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int nProvinceId = Integer.parseInt(request.getParameter("nProvinceId"));
		List<Map<String, Object>> sCitylist = areaManagerService
				.selectsCitybyProvince(nProvinceId);
		ObjectMapper objm = new ObjectMapper();
		String result = objm.defaultPrettyPrintingWriter().writeValueAsString(
				sCitylist);
		response.getWriter().print(result);
	}
}
